package com.py.pythink.service.impl;

import com.alibaba.fastjson.JSON;
import com.py.pythink.bo.BrowseBo;
import com.py.pythink.config.RedisKeyConfig;
import com.py.pythink.dao.BrowseDao;
import com.py.pythink.dao.CollectDao;
import com.py.pythink.entity.User;
import com.py.pythink.service.intf.BrowseService;
import com.py.pythink.third.JedisUtil;
import com.py.pythink.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author jls
 * @date 2021/7/7 21:24
 */
@Service
public class BrowseServiceImpl implements BrowseService {

    @Autowired
    private BrowseDao dao;
    /**
     * 根据令牌得到用户的id 再用id查询该用户租 的房子信息列表
     * @param token
     * @return
     */
    @Override
    public R queryall(String token) {
        //1.根据令牌得到
        String key = RedisKeyConfig.USER_TOKEN+token;
        if(JedisUtil.checkKey(key)){
            //根据令牌得到User的对象 进而得到Uid
            User user = JSON.parseObject(JedisUtil.getStr(key),User.class);
            int uid = user.getId();
            //根据用户的id可以得到此用户浏览的所有房子的fid
            List<Integer> findfid = dao.findfid(uid);
            List<BrowseBo> list = new ArrayList<>();
            for(int f:findfid){
                //通过三表查询 得到BrowseBo中的属性
                BrowseBo finddetail = dao.findBrowseBo(f);
                list.add(finddetail);
            }
            return R.ok(list);
        }
        return R.fail();
    }

    @Override
    public R delete(int fid) {

        if(fid>0){
           if(dao.update1(fid)>0){
               return R.ok("删除成功！");
           }
        }
        return R.fail();
    }
}
